package com.uenpay.im.ui.old.activity.bankcard;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.BroadcastAction;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.callback.IOnBottomDialogListener;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.DialogUtility;
import com.uenpay.im.weight.ZProgressHUD;
import com.uenpay.util.app.base.BaseActivity;

import java.util.ArrayList;

/**
 * @author SL
 * @version 1.0
 * @copyRight http://www.qiyunxin.com
 * @date 2016-6-12 下午3:28:28
 * @function 我的银行卡
 */
public class MyBankCardActivity extends BaseActivity {

    private MyBroadCast myBroadCast;
    private LinearLayout bank_card_layout;
    private ZProgressHUD mZProgressHUD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_my_bankcard_layout;
    }

    @Override
    public void initView(View view) {
        initView();
        initListener();
        myBankCard();
    }

    protected void initView() {
        mZProgressHUD = new ZProgressHUD(this);
        ((TextView) findViewById(R.id.title_textview))
                .setText(R.string.my_bank_card);
        ((ImageView) findViewById(R.id.title_right_add_iv))
                .setImageResource(R.drawable.ic_add);
        findViewById(R.id.title_right_iv_layout).setVisibility(View.VISIBLE);
        bank_card_layout = (LinearLayout) findViewById(R.id.bank_card_layout);
    }

    protected void initListener() {
        findViewById(R.id.title_right_iv_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        Intent intent = new Intent(MyBankCardActivity.this,
                                AddBankCardActivity.class);
                        startActivity(intent);
                    }
                });
    }

    private void myBankCard() {
        mZProgressHUD.show();
        BankCardHandle.myBankCard(new BankCardHandle.IMyBankCardListener() {

            @Override
            public void onMyBankCardResult(int status, String msg, final ArrayList<BankEntity> arrayList) {
                mZProgressHUD.dismiss();
                if (status == HttpInvokeResultCode.RESULT_OK) {
                    bank_card_layout.removeAllViews();
                    if (arrayList != null && arrayList.size() > 0) {
                        for (int i = 0, size = arrayList.size(); i < size; i++) {
                            bank_card_layout.addView(getView(arrayList.get(i)));
                        }
                    } else {
                        findViewById(R.id.title_right_iv_layout).setVisibility(
                                View.VISIBLE);
                    }
                } else if (!TextUtils.isEmpty(msg)) {
                    IMApplication.showToast(msg);
                }
            }
        });
    }

    private View getView(final BankEntity bankEntity) {
        View view = View.inflate(MyBankCardActivity.this,
                R.layout.item_bank_card_layout, null);
        TextView typeName = (TextView) view
                .findViewById(R.id.bank_card_type_name);
        TextView cardName = (TextView) view.findViewById(R.id.bank_card_name);
        TextView accountName = (TextView) view.findViewById(R.id.bank_account_name);
        typeName.setText(bankEntity.bank_name);
        accountName.setText(bankEntity.holder_name);
        if (!TextUtils.isEmpty(bankEntity.card_no)) {
            cardName.setText("**** **** **** "
                    + bankEntity.card_no.substring(bankEntity.card_no.length() - 3, bankEntity.card_no.length()));
        }

        view.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View arg0) {
                showDeleteDialog(bankEntity.id);
                return false;
            }
        });
        return view;
    }


    private void showDeleteDialog(final int id) {
        DialogUtility.showDialog(MyBankCardActivity.this,
                R.string.delete_bank_card, R.string.sure, R.string.cancel,
                true, new IOnBottomDialogListener() {

                    @Override
                    public void onClicked() {
                        String url = APIConfiguration.getPayUrl() + "users/" + IMApplication.getInstance().getUcrOpendId() + "/bankcards/" + id;
                        deleteBankCard(url);
                    }
                });
    }

    private void deleteBankCard(String url) {
        mZProgressHUD.show();
        BankCardHandle.deleteBankCard(url,
                new BankCardHandle.IBankCardListener() {

                    @Override
                    public void onBankCardResult(int status, String msg) {
                        myHandler.sendEmptyMessage(1);
                        if (!TextUtils.isEmpty(msg)) {
                            IMApplication.showToast(msg);
                        }
                        if (status == HttpInvokeResultCode.RESULT_OK) {
                            myHandler.sendEmptyMessage(0);
                        }
                    }
                });

    }


    private Handler myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 0) {
                bank_card_layout.removeAllViews();
                myBankCard();
            } else if (msg.what == 1) {
                mZProgressHUD.dismiss();
            }
            return false;
        }
    });

//
//
//	private void deleteBankCard(String bankcard, String paypassword) {
//		mZProgressHUD.show();
//		BankCardHandle.deleteBankCard(bankcard, paypassword,
//				new BankCardHandle.IBankCardListener() {
//
//					@Override
//					public void onBankCardResult(int status, String msg) {
//						mZProgressHUD.dismiss();
//						if(!TextUtils.isEmpty(msg)){
//							IMApplication.showToast(msg);
//						}
//						if (status == HttpInvokeResultCode.RESULT_OK) {
//							myBankCard();
//						}
//					}
//				});
//
//	}

    private class MyBroadCast extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            if (arg1.getAction().equals(
                    BroadcastAction.ACTION_REFRESH_BANK_CARD)) {
                myBankCard();
            }
        }
    }

    @Override
    protected void onResume() {
        if (myBroadCast == null) {
            myBroadCast = new MyBroadCast();
            IntentFilter filter = new IntentFilter();
            filter.addAction(BroadcastAction.ACTION_REFRESH_BANK_CARD);
            registerReceiver(myBroadCast, filter);
        }
        super.onResume();
    }

    @Override
    public void onBackPressed() {
        finish();
        super.onBackPressed();
    }
}
